/**
 * Created by su on 2017/3/28.
 */
$(function () {
    //地图 start
    var address;
    // 百度地图API功能
    function G(id) {
        return document.getElementById(id);
    }// 百度地图API功能

    $('#map').click(function(){
        $('.map-page').show();
        $("#all-content").hide();
        $(".i-date-top-font").text("地图");
        $("#share").hide();
        $(".invitation-remind").hide();
        // 百度地图API功能
        var map = new BMap.Map("allmap");
        map.centerAndZoom("上海",12);
        map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
        map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function(r){
            if(this.getStatus() == BMAP_STATUS_SUCCESS){
                var mk = new BMap.Marker(r.point);
                map.addOverlay(mk);
                map.panTo(r.point);
                //alert('您的位置：'+r.point.lng+','+r.point.lat);
            }
            else {
                alert('failed'+this.getStatus());
            }
        },{enableHighAccuracy: true})
        //单击获取点击的经纬度
        map.addEventListener("click",function(e){
            var point = new BMap.Point(e.point.lng,e.point.lat);
            var gc = new BMap.Geocoder();
            gc.getLocation(point, function(rs) {

                var addComp = rs.addressComponents;
                var mapAddress = addComp.province+addComp.city + addComp.district
                    + addComp.street + addComp.streetNumber;
                $("#suggestId").val(mapAddress);
            });
        });
        var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
            {"input" : "suggestId"
                ,"location" : map
            });

        ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
            var str = "";
            var _value = e.fromitem.value;
            var value = "";
            if (e.fromitem.index > -1) {
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }
            str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }
            str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
            G("searchResultPanel").innerHTML = str;
        });

        var myValue;
        ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
            var _value = e.item.value;
            myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

            setPlace();
        });
        var myGeo = new BMap.Geocoder();//地址解析
        function setPlace(text){
            map.clearOverlays();    //清除地图上所有覆盖物
            function myFun(){
                if(text == undefined){
                    var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                    map.centerAndZoom(pp, 18);
                    map.addOverlay(new BMap.Marker(pp));    //添加标注
                } else {
                    myGeo.getPoint(text,function (point) {
                        if(point){
                            console.log(point);
                            map.centerAndZoom(point, 18);
                            map.addOverlay(new BMap.Marker(point));    //添加标注
                        } else {
                            alert("请输入准确地址，方便我们定位，谢谢");
                        }
                    });
                }

            }
            var local = new BMap.LocalSearch(map, { //智能搜索
                onSearchComplete: myFun
            });
            local.search(myValue);
        }

        $('#search-add').click(function () {
            address = $('#suggestId').val();
            setPlace(address);
        });
        $('#confirm').click(function () {
                $("#invitation-add").val($('#suggestId').val());
                $('.map-page').hide();
                $("#all-content").show();
                $(".i-date-top-font").text("创建时间");
                $("#share").show();
                $(".invitation-remind").show();
                // waitResponses页面的编辑地图
                $("#wait-edit-map").val($('#suggestId').val());
            }
        );

        //地图搜索和确定按钮 固定 start
        $('.map-search-top').scrollToFixed({
            marginTop: 50
        });
        $('.map-btn').scrollToFixed( {
            bottom: 0
        });
        //地图搜索和确定按钮 固定 end
    });
    //地图end


    //时长 start
    // $('#slider1').range2DSlider({
    //     template:'horizontal',
    //    	value:[[1,1],[0,0]],
    //     //value:[1,0],
    //     onlyGridPoint:true,
    //     round:true,
    //     axis:[[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]]
    // });
    // var init_value = 1;
    // $( "#slider-range-min" ).slider({
    //     range: "min",
    //     value:init_value,
    //     min: 0,
    //     max: 24,
    //     slide: function( event, ui ) {
    //         $(".drop_time").text(ui.value);//拖动事件
    //         //小箭头样式
    //         if(ui.value >= 10) {
    //             $(".ui-slider-horizontal .ui-slider-handle").css('margin-left','-1.2em');
    //         } else {
    //             $(".ui-slider-horizontal .ui-slider-handle").css('margin-left','-1.8em');
    //         }
    //
    //     }
    // });
    // $(".drop_time").text(init_value);//初始化

    var mySlider = new Slider("#ex8", {
        tooltip: 'always',
        formatter : function (e) {
            return e +'小时';
        },
    });
    $("#ex8").on('slideStop', function (e) {
        console.log(e.value);
    });

    //时长 end

    //日历start
    $(".form_datetime").datetimepicker({format: 'yyyy-mm-dd hh:ii'});
    //日历end


    var color = [ '#1AA9BA','#FE0100','#DDA069','#FCC626','#07FD00','#00FFFF','#0000FE','#FF00FF','#A6A6A6','#000000'];
    var tmp = `<li role="presentation">
                    <div class="bg-color checked"><ins style="background-color:` + color[0] + `" class="radio_color1"></ins></div>
                </li>`;
    //这里的循环输出是根据css中color的个数输出
    for(var i=1;i<color.length;i++) {
        tmp += `<li role="presentation">
                    <div class="bg-color"><ins class="radio_color1" style="background-color:` + color[i] + `" ></ins></div>
                </li>`
    }

    $('#radio_color').html(tmp)

    $('.bg-color').on('click', function(){
        $('.bg-color').removeClass('checked');
        var titleColor = $(this).find('ins').css('background');
        $(".chose-color button").css("background",titleColor);
        $(this).addClass('checked');
    })
    // 标题颜色切换end
});
